<%-- 
    Document   : adminUserControl
    Created on : Apr 8, 2011, 4:38:11 PM
    Author     : Robert Gunawan
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="jesstalk.Db"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<script type="text/javascript" src="script/ajaxwrap.js"></script>
<script type= "text/javascript">
    aj = new AjaxWrap(); //bikin aj sebagai AjaxWrap
    aj.onLoadSuccess = function (ajWrap){

    }
</script>

<%
if((session.getAttribute("username") == null)||(!session.getAttribute("username").equals("admin"))) //ilegal access
        {
            response.sendRedirect("index.jsp");
        }
Db Admin = Db.get(getServletContext());
ArrayList<String> ListUsername = new ArrayList<String>();

//==================================AREA LOG OUT============================================
if(request.getParameter("Logout") != null)
    {
    session.removeAttribute("username");
    session.removeAttribute("kategoriBefore");
    session.removeAttribute("kategoriAfter");
    response.sendRedirect("index.jsp");
    }

//===============================AREA BANNED USER============================================
boolean CekBanned = true;
String UsernameBanned ="";
String BannedState ="";
if(request.getParameter("Username")== null)
    {
    CekBanned = false;
    }
else
    UsernameBanned = request.getParameter("Username");
if(request.getParameter("Banned") == null)
    {
    CekBanned = false;
    }
else
    BannedState = request.getParameter("Banned");
if(CekBanned == true) //ada yang harus di banned
    {
    String Query = "select * from user where username ='" + UsernameBanned+ "'";
    ArrayList<String> usernameBlockArray = Admin.getQuery("jesstalk", Query, "username");
        if(usernameBlockArray.size() != 1) //kondisi gagal
            {
                %>
                <script type ="text/javascript">
                    alert("No username found!");
                </script>
                <%
            }
        else
            {
            //UPDATE `jesstalk`.`user` SET `password` = 'meong' WHERE `user`.`id` =9 LIMIT 1 ;
            Query = "update user set isBanned = '" +BannedState + "' where username='"+ usernameBlockArray.get(0)+ "'";
            Admin.updateFrom("jesstalk", Query);
            //mulai ajax ngebanned user
            %>
            <script type="text/javascript">
                aj.openAndSend("GET", "ChatServlet?banuserfromsystem="+<%out.print("\""+usernameBlockArray.get(0)+"\"");%>, true, null);
            </script>
            <%
            }
    }

//===============================AREA DELETE USER============================================
boolean CekDelete = true;
String UsernameDelete ="";
boolean Delete = false;
if(request.getParameter("UsernameDel")== null)
    {
    CekDelete = false;
    }
else
    UsernameDelete = request.getParameter("UsernameDel");
if(request.getParameter("Delete") == null)
    {
    CekDelete = false;
    }
else
    Delete = true;

if((CekDelete == true) && (Delete == true)) //ada yang didelete
    {
    String Query = "delete from user where username='"+UsernameDelete+"'";
    String Query2 = "select * from user where username ='"+UsernameDelete+"'";
    ArrayList<String>idDelete = Admin.getQuery("jesstalk", Query2, "id");
    Admin.deleteFrom("jesstalk", Query);
    %>
            <script type="text/javascript">
                aj.openAndSend("GET", "ChatServlet?deluserfromsystem="+<%out.print("\""+UsernameDelete+"\"");%>, true, null);
            </script>
    <%
    //hapus user dari tabel moderasi
    String Query3 = "delete from moderasi where idUser ='"+ idDelete.get(0)+"'";
    Admin.deleteFrom("jesstalk", Query3);
    //hapus user dari tabel friend
    String Query4 = "delete from friend where idFollower='"+idDelete.get(0)+"'";
    String Query5 = "delete from friend where idFollowing='"+idDelete.get(0)+"'";
    Admin.deleteFrom("jesstalk", Query4);
    Admin.deleteFrom("jesstalk", Query5);
    }

//===================================AREA MAX ROOM========================================
boolean CekMax = true;
String Value = "";
if(request.getParameter("Max") == null)
    {
    CekMax = false;
    }
else
    Value = request.getParameter("Max");
if(CekMax == true)
    {
    String Query = "update user set nRoom = '" +Value + "' where username='"+ request.getParameter("NamaRoom")+ "'";
    Admin.updateFrom("jesstalk", Query);
    //out.println(Query);
    }

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin User Controller Page</title>
        <link href="style/styleAdmin.css" rel="stylesheet" type="text/css">
        
    </head>
    <body>
        <div id="DivLogout">
            <a href="adminUserControl.jsp?Logout=1">Log Out</a>
        </div>
        <div id="MenuAdmin">
            <!--Ntar diisi oleh menu-menu admin, ada 2 keknya, bagian kategori ama user!-->
            List Option :
            <a href="adminCategory.jsp">Category Setting</a>
        </div>
        <div id ="judulAdmin">
        <h1>Admin User Controller Page</h1>
        </div>
        <div id ="content">
            <!-- Content User !-->
            <div id="contentUser">
            <table id="table1">
                <tr class="bdr">
                    <td class="bdr">Name</td>
                    <td class="bdr">Ban?</td>
                    <td class="bdr">Delete?</td>
                    <td class="bdr">Set Max Room</td>
                </tr>
                    <%
                    String Query = "select * from user";
                    ListUsername = Admin.getQuery("jesstalk", Query, "username");
                    int i;
                    //remove nama admin dl
                    int Max = ListUsername.size();
                    for(i=0;i<Max;i++)
                        {
                        String QueryAdmin = "select * from user where username ='"+ ListUsername.get(i)+"'";
                        ArrayList<String> StatusAdmin = Admin.getQuery("jesstalk", QueryAdmin, "isAdmin");
                        if(StatusAdmin.get(0).equals("1"))
                            {
                            ListUsername.remove(i);
                            i--;
                            Max--;
                            }
                        }
                    for(i=0;i<ListUsername.size();i++)
                        {
                        %>
                        <tr class="bdr">
                            <!-- Penampil username!-->
                            <td class="bdr">
                                <%
                                out.println(ListUsername.get(i));
                                %>
                            </td>

                            <!-- Penampil status banned !-->
                            <td class="bdr">
                                <%
                                //out.println("<a href = 'http://www.google.com'>Google </a>");
                                String QueryBanned = "select * from user where username ='"+ ListUsername.get(i)+"'";
                                ArrayList<String> StatusBanned = Admin.getQuery("jesstalk", QueryBanned, "isBanned");
                                if(StatusBanned.get(0).equals("0")) //belum kebanned
                                    {
                                    out.println("<a href = 'adminUserControl.jsp?Username="+ListUsername.get(i)+"&Banned=1'>Ban User</a>");
                                    }
                                else //status banned
                                    {
                                    out.println("<a href = 'adminUserControl.jsp?Username="+ListUsername.get(i)+"&Banned=0'>Unband User</a>");
                                    }
                                %>
                            </td>

                            <!-- Bagian Delete User!-->
                            <td class="bdr">
                                <%
                                    out.println("<a href = 'adminUserControl.jsp?UsernameDel="+ListUsername.get(i)+"&Delete=1'>Delete User</a>");
                                %>
                            </td>

                            <!-- Bagian Set Max Room !-->
                            <td class="bdr">
                                <%
                                ArrayList<String> MaxRooms = new ArrayList<String>();
                                String QueryM = "select * from user where username = '"+ListUsername.get(i)+"'";
                                MaxRooms = Admin.getQuery("jesstalk", QueryM, "nRoom");
                                %>
                                <form id="MaxRoom" name="MaxRoom" method="post" action="#">
                                    <input type="hidden" value="<%out.print(ListUsername.get(i)); %>" id="NamaRoom" name="NamaRoom" />
                                    <input type="text" value="<%out.print(MaxRooms.get(0)); %>" id="Max" name="Max" />
                                    <input type="submit" id="MaxSubmit" name="MaxSubmit" />
                                </form>
                            </td>
                        </tr>
                        <%
                        }
                    %>
            </table>
        </div>
        </div>
            <div id="tesAjax">
                
            </div>
    </body>
</html>
